import { DamageResult, ITriggerResult } from "../../new/trigger/const/ITriggerResult";
import { TriggerMgr } from "../../new/trigger/TriggerMgr";
import { BattleRoleUI } from "../roleUI/BattleRoleUI";
import { ActionBase } from "./actions/ActionBase";
import { UITriggerType } from "./UITriggerType";

/**
 * 界面行为表现触发管理器(界面的表现也通过触发器触发，不需要各种回调)
 */
export class UITriggerMgr {
    private triggerActions: {};

    constructor() {
        this.triggerActions = {};
    }

    registerTrigger(type: UITriggerType, action: ActionBase) {
        if (!this.triggerActions[type]) {
            this.triggerActions[type] = []
        }
        this.triggerActions[type].push(action)
    }

    unregisterTrigger(trigger) {
        let list = this.triggerActions[trigger.triggerType];
        if (list) {
            const index = list.indexOf(trigger);
            if (index != -1) {
                list.splice(index, 1);
            }
        }
    }

    trigger(eventType, args?: { caster: BattleRoleUI, target: BattleRoleUI }) {
        let actions: ActionBase[] = this.triggerActions[eventType];
        if (actions) {
            while (actions.length > 0) {
                let action = actions.shift();
                action.excute()
            }
        }
    }

    /**根据条件触发 */
    triggerByParam(eventType, params: any) {
        let actions: ActionBase[] = this.triggerActions[eventType];
        if (actions) {
            for (let i = 0; i < actions.length; i++) {
                let action = actions[i];
                if (action.check(params)) {
                    action.excute();
                    actions.splice(i, 1);
                    i--;
                }
            }
        }
    }
}